package cn.zbh.user.controller;


import cn.zbh.basic.exception.BusinessException;
import cn.zbh.basic.util.AjaxResult;
import cn.zbh.user.dto.LoginDto;
import cn.zbh.user.dto.WxDto;
import cn.zbh.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto dto){
        try {
           Map<String,Object> map= loginService.account(dto);
           return AjaxResult.me().setResultObj(map);
        }  catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("网络异常");
        }
    }

    /**
     * 微信登录
     * @param
     * @return
     */

    @GetMapping("/weChat/{code}")
    public AjaxResult weChat(@PathVariable("code") String code){


        try {

            return loginService.weChat(code);
        }  catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("网络异常");
        }
    }

    @PostMapping ("/wechat/binder")
    public AjaxResult binder(@RequestBody WxDto dto){


        try {

            return loginService.binder(dto);
        }  catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("网络异常");
        }
    }
}
